package orderProject

import (
	"time"
	"web/models"
	"web/pkg/util"
)

// ROrderProject 订单项目详细响应结构
type ROrderProject struct {
	Id         uint    `json:"id"`
	OrderId    uint    `json:"order_id"`
	OrderCode  string  `json:"order_code"`
	ProjectId  int     `json:"project_id"`
	Project    string  `json:"project"`
	Price      float64 `json:"price"`
	Duration   int     `json:"duration"`
	Count      int     `json:"count"`
	TotalPrice float64 `json:"total_price"` // 计算字段：价格*数量
	TotalTime  int     `json:"total_time"`  // 计算字段：时长*数量
	CreatedAt  string  `json:"created_at"`
}

// RLintOrderProject 订单项目简要响应结构
type RLintOrderProject struct {
	Id        uint    `json:"id"`
	OrderId   uint    `json:"order_id"`
	OrderCode string  `json:"order_code"`
	Project   string  `json:"project"`
	Price     float64 `json:"price"`
	Count     int     `json:"count"`
}

type EncodeOrderProjectService struct {
}

// NewEncodeOrderProjectService 创建编码服务实例
func NewEncodeOrderProjectService() *EncodeOrderProjectService {
	return &EncodeOrderProjectService{}
}

// Encode 编码单个订单项目
func (s *EncodeOrderProjectService) Encode(project *models.LaOrderProject) *ROrderProject {
	if project == nil {
		return &ROrderProject{}
	}

	totalPrice := project.Price * float64(project.Count)
	totalTime := project.Duration * project.Count

	return &ROrderProject{
		Id:         uint(project.ID),
		OrderId:    project.OrderID,
		OrderCode:  project.OrderCode,
		ProjectId:  project.ProjectID,
		Project:    project.Project,
		Price:      project.Price,
		Duration:   project.Duration,
		Count:      project.Count,
		TotalPrice: totalPrice,
		TotalTime:  totalTime,
		CreatedAt:  s.formatTimestamp(project.CreatedAt),
	}
}

// EncodeLint 编码简要订单项目信息
func (s *EncodeOrderProjectService) EncodeLint(project *models.LaOrderProject) *RLintOrderProject {
	if project == nil {
		return &RLintOrderProject{}
	}

	return &RLintOrderProject{
		Id:        uint(project.ID),
		OrderId:   project.OrderID,
		OrderCode: project.OrderCode,
		Project:   project.Project,
		Price:     project.Price,
		Count:     project.Count,
	}
}

// EncodeList 编码订单项目列表
func (s *EncodeOrderProjectService) EncodeList(projects []*models.LaOrderProject) []*ROrderProject {
	if len(projects) == 0 {
		return make([]*ROrderProject, 0)
	}

	result := make([]*ROrderProject, len(projects))
	for i, project := range projects {
		result[i] = s.Encode(project)
	}
	return result
}

// EncodeLintList 编码简要订单项目列表
func (s *EncodeOrderProjectService) EncodeLintList(projects []*models.LaOrderProject) []*RLintOrderProject {
	if len(projects) == 0 {
		return make([]*RLintOrderProject, 0)
	}

	result := make([]*RLintOrderProject, len(projects))
	for i, project := range projects {
		result[i] = s.EncodeLint(project)
	}
	return result
}

// MakePagedResponse 创建分页响应
func (s *EncodeOrderProjectService) MakePagedResponse(projects []*models.LaOrderProject, nextPageInfo util.NextPageInfo) map[string]interface{} {
	encodedProjects := s.EncodeList(projects)
	return util.MakePagedResponse(encodedProjects, nextPageInfo)
}

// MakePagedLintResponse 创建分页简要响应
func (s *EncodeOrderProjectService) MakePagedLintResponse(projects []*models.LaOrderProject, nextPageInfo util.NextPageInfo) map[string]interface{} {
	encodedProjects := s.EncodeLintList(projects)
	return util.MakePagedResponse(encodedProjects, nextPageInfo)
}

// formatTimestamp 格式化时间戳
func (s *EncodeOrderProjectService) formatTimestamp(timestamp time.Time) string {
	if timestamp.IsZero() {
		return ""
	}
	return timestamp.Format("2006-01-02 15:04:05")
}
